package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class AddOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        //判满
        int bookSize = bookList.getBookSize();
        Book[] books = bookList.getBooks();
        if(bookSize == books.length) {
            System.out.println("书架已满，无法添加！");
            return;
        }
        //构造对象
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入需要添加书的名字：");
        String name = scanner.nextLine();
        System.out.println("请输入该本书的作者：");
        String author = scanner.nextLine();
        System.out.println("请输入该本书的价格：");
        double price = scanner.nextInt();
        scanner.nextLine();
        System.out.println("请输入该本书的类型：");
        String type = scanner.nextLine();

        //判断是否已有这本书
        for (int i = 0; i < bookSize; i++) {
            if(books[i].getName() == name) {
                System.out.println("书架上已经有这本书了！");
                return;
            }
        }

        //将新书加入书架
        Book newBook = new Book(name,author,price,type);
        bookList.setBooks(newBook);
        System.out.println("添加成功！");
    }
}
